package com.ruoyi.web.controller.robot;

import java.util.List;

import com.ruoyi.framework.shiro.service.SysTokenService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RobotInfo;
import com.ruoyi.system.service.IRobotInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 机器人信息Controller
 *
 * @author ruoyi
 * @date 2025-10-22
 */
@Controller
@RequestMapping("/robot/info")
public class RobotInfoController extends BaseController {
    private String prefix = "system/info";

    @Autowired
    private IRobotInfoService robotInfoService;

    @Autowired
    private SysTokenService sysTokenService;

    @RequiresPermissions("robot:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询机器人信息列表
     */
    @RequiresPermissions("robot:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RobotInfo robotInfo) {
        startPage();
        List<RobotInfo> list = robotInfoService.selectRobotInfoList(robotInfo);
        return getDataTable(list);
    }

    /**
     * 查询机器人信息列表: 用于API调用
     */
    @GetMapping("/api/list")
    @ResponseBody
    // 修改返回类型为 AjaxResult
    public AjaxResult apiList(RobotInfo robotInfo) {
        // 检查API Token是否存在且有效
        String token = getAPIToken();
        boolean isTokenValid = checkAPITokenValid(token);
        if (!isTokenValid && !sysTokenService.existsApiToken(token)) {
            return AjaxResult.error("API Token不存在、无效或者已经过期！");
        }
//        startPage();
        List<RobotInfo> list = robotInfoService.selectRobotInfoList(robotInfo);
        // 将原TableDataInfo数据封装到AjaxResult中返回
//        TableDataInfo dataTable = getDataTable(list);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("totalCount", list.size());
        ajaxResult.put("data", list);
        return ajaxResult;
    }

    /**
     * 导出机器人信息列表
     */
    @RequiresPermissions("robot:info:export")
    @Log(title = "机器人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RobotInfo robotInfo) {
        List<RobotInfo> list = robotInfoService.selectRobotInfoList(robotInfo);
        ExcelUtil<RobotInfo> util = new ExcelUtil<RobotInfo>(RobotInfo.class);
        return util.exportExcel(list, "机器人信息数据");
    }

    /**
     * 新增机器人信息
     */
    @RequiresPermissions("robot:info:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存机器人信息
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "机器人信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RobotInfo robotInfo) {
        return toAjax(robotInfoService.insertRobotInfo(robotInfo));
    }

    /**
     * 修改机器人信息
     */
    @RequiresPermissions("robot:info:edit")
    @GetMapping("/edit/{robotId}")
    public String edit(@PathVariable("robotId") Long robotId, ModelMap mmap) {
        RobotInfo robotInfo = robotInfoService.selectRobotInfoByRobotId(robotId);
        mmap.put("robotInfo", robotInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存机器人信息
     */
    @RequiresPermissions("robot:info:edit")
    @Log(title = "机器人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RobotInfo robotInfo) {
        return toAjax(robotInfoService.updateRobotInfo(robotInfo));
    }

    /**
     * 删除机器人信息
     */
    @RequiresPermissions("robot:info:remove")
    @Log(title = "机器人信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(robotInfoService.deleteRobotInfoByRobotIds(ids));
    }
}
